<?php
class RegAction extends CommonAction{
	function _initialize() {
		$this->_inject_check(0);//调用过滤函数
		$this->_Config_name();
		header("Content-Type:text/html; charset=utf-8");
	}

    /**
     * 会员注册
     * **/
    public function users($Urlsz=0){
        $this->_checkUser();
        $fck = M ('fck');
        $fee = M ('fee');

        $RID = (int) $_GET['RID'];
        $FID = (int) $_GET['FID'];
        $TP = (int) $_GET['TPL'];
        if (empty($TPL))$TPL = 0;
        $TPL = array();
        for($i=0;$i<5;$i++){
            $TPL[$i] = '';
        }
        $TPL[$TP] = 'selected="selected"';

        //===报单中心
        $zzc = array();
        $where = array();
        $where['id'] = $_SESSION[C('USER_AUTH_KEY')];
        $field ='user_id,is_agent,agent_cash,shop_name,re_path';
        $rs = $fck ->where($where)->field($field)->find();
        $money = $rs['agent_cash'];
        $mmuserid = $rs['user_id'];
        if ($rs['is_agent'] >= 2){
            $zzc[1] = $rs['user_id'];
        }else{
            //优先选择最近的报单中心
            $re_p = M('fck')->where("id in (0{$rs['re_path']}0) and is_agent=2")->order('re_level desc')->find();
            if ($re_p){
                $zzc[1] = $re_p['user_id'];
            }else{
                $mrs = M('fck')->where('id=1')->field('id,user_id')->find();
                $zzc[1] = $mrs['user_id'];
            }
        }
        $this->assign('myid',$_SESSION[C('USER_AUTH_KEY')]);

        //===推荐人
        $where['id'] = $RID;
        $field ='user_id,is_agent';
        $rs = $fck ->where($where)->field($field)->find();
        if ($rs){
            $zzc[2] = $rs['user_id'];
        }else{
            $zzc[2] = $mmuserid;
        }
        //$zzc[2] = $mmuserid;
        //===接点人
        $where['id'] = $FID;
        $field ='user_id,is_agent';
        $rs = $fck ->where($where)->field($field)->find();
        if ($rs){
            $zzc[3] = $rs['user_id'];
        }else{
            $zzc[3] = '';
        }

        //会员级别
        $HYJJ="";
        $this->_levelConfirm($HYJJ,1);
        $this->assign('HYJJ',$HYJJ);//会员级别


        $arr = array();
        $arr['UserID'] = $this->_getUserID();
        $this->assign('flist',$arr);

        $pwhere = array();
        $product = M ('product');
        $pwhere['is_reg'] = array("eq",1);
        $prs = $product->where($pwhere)->select();
        $this->assign('plist',$prs);



        $fee_s = $fee->field('*')->find();
        $s9 = $fee_s['s9'];
        $s9 = explode('|',$s9);
        $s2 = explode('|',$fee_s['s2']);

        //是否关闭注册
        $close_if=$fee_s['i5'];
        if(!$close_if){ $this->error('网站暂时关闭注册功能，请与管理员联系！');}

        $i4 = $fee_s['i4'];
        if ($i4==0){
            $openm=1;
        }else{
            $openm=0;
        }
        //输出银行
        $bank = explode('|',$fee_s['str29']);
        //输出级别名称
        $Level = explode('|',C('Member_Level'));
        //输出注册单数
        $Single = explode('|',C('Member_Single'));
        //输出一单的金额

        $lang= explode('|',$fee_s['str24']);
        $countrys = explode('|',$fee_s['str25']);

        $wentilist = explode('|',$fee_s['str99']);

        $this->assign('s9',$s9);
        $this->assign('s2',$s2);
        $this->assign('openm',$openm);
        $this->assign('bank',$bank);
        $this->assign('Level',$Level);
        $this->assign('Single',$Single);
        $this->assign('Money',$fee_s['s2']);
        $this->assign('Money1',$money);
        $this->assign('wentilist',$wentilist);

        $this->assign('lang',$lang);
        $this->assign('countrys',$countrys);

        unset($bank,$Level,$Level);

        $this->assign('TPL',$TPL);
        $this->assign('zzc',$zzc);

        unset($fck,$TPL,$where,$field,$rs,$data_temp,$temp_rs,$rs);
        $this->display('users');
    }

    /**
     * 注册确认
     * **/
    public function usersConfirm(){
        $this->_checkUser();
        $id = $_SESSION[C('USER_AUTH_KEY')];
        $fck    = M ('fck');
        $rs = $fck -> field('is_pay,agent_cash') -> find($id);
        if($rs['is_pay'] == 0){
            $this->error('临时会员不能注册会员！');
            exit;
        }
        if (strlen($_POST['UserID'])<1){
            $this->error('会员编号不能少！');
            exit;
        }
        //if(!preg_match("/^1[34578]\d{9}$/",$_POST['UserID'])){
//			$this->error('会员编号必须是手机号码格式！');
//			exit;
//		}

        $this->assign('UserID',$_POST['UserID']);

        $data = array();  //创建数据对象
        $shopid = trim($_POST['shopid']);  //所属报单中心帐号
        if (empty($shopid)){
            $this->error('请输入报单中心编号！');
            exit;
        }
        $smap = array();
        $smap['user_id'] = $shopid;
        $smap['is_agent'] = array('gt',1);
        $shop_rs = $fck->where($smap)->field('id,user_id')->find();
        if (!$shop_rs){
            $this->error('没有该报单中心！');
            exit;
        }
        $this->assign('shopid',$shopid);
        unset($smap,$shop_rs,$shopid);

        //检测推荐人
        $RID = trim($_POST['RID']);  //获取推荐会员帐号
        $mapp  = array();
        $mapp['user_id']	= $RID;
        $mapp['is_pay']	    = array('gt',0);
        $authInfoo = $fck->where($mapp)->field('id,user_id,re_level,re_path')->find();
        if ($authInfoo){
            $this->assign('RID',$RID);
            $data['re_id'] = $authInfoo['id'];
        }else{
            $this->error('推荐人不存在！');
            exit;
        }
        unset($authInfoo,$mapp);

        //检测上节点人
        //$FID = trim($_POST['FID']);  //上节点帐号
// 		$mappp  = array();
// 		$mappp['user_id'] = $FID;
// 		$authInfoo = $fck->where($mappp)->field('id,p_path,p_level,user_id,is_pay,tp_path')->find();
// 		if ($authInfoo){
// 			$this->assign('FID',$FID);
// 			$fatherispay = $authInfoo['is_pay'];
// 			$data['father_id'] = $authInfoo['id'];                        //上节点ID
// 			$tp_path = $authInfoo['tp_path'];
// 		}else{
// 			$this->error('上级会员不存在！');
// 			exit;
// 		}
// 		unset($authInfoo,$mappp);
//  		$TPL = (int)$_POST['TPL'];
// 		$where = array();
// 		$where['father_id'] = $data['father_id'];
// 		$where['treeplace'] = $TPL;
// 		$rs = $fck->where($where)->field('id')->find();
// 		if ($rs){
// 			$this->error('该位置已经注册！');
// 			exit;
// 		}
// 		if($TPL==0){
// 			$zy_n = "1区";
// 		}elseif($TPL==1){
// 			$zy_n = "2区";
// 		}elseif($TPL==2){
// 			$zy_n = "3区";
// 		}else{
// 			$TPL = 0;
// 			$zy_n = "1区";
// 		}
// 		$this->assign('zy_n',$zy_n);
// 		$this->assign('TPL',$TPL);
//
// 		if($fatherispay==0&&$TPL>0){
// 			$this->error('接点人开通后才能在此位置注册！');
// 			exit;
// 		}
//
//		$renn = $fck->where('re_id='.$data['re_id'].' and is_pay>0')->count();
//		if($renn<1){
//			$tjnn = $renn+1;
//			if($renn==0){
//				$oktp = 0;
//				$errtp = "左区";
//			}
//			$zz_id = $this->pd_left_us($data['re_id'],$oktp);
//			$zz_rs = $fck->where('id='.$zz_id)->field('id,user_id')->find();
//			if($zz_id!=$data['father_id']){
//				$this->error('推荐第'.$tjnn.'人必须放在'.$zz_rs['user_id'].'的'.$errtp.'！');
//				exit;
//			}
//			if($TPL!=$oktp){
//				$this->error('推荐第'.$tjnn.'人必须放在'.$zz_rs['user_id'].'的'.$errtp.'！');
//				exit;
//			}
//		}
//		unset($rs,$where,$TPL);

        $fwhere = array();//检测帐号是否存在
        $fwhere['user_id'] = trim($_POST['UserID']);
        $frs = $fck->where($fwhere)->field('id')->find();
        if ($frs){
            $this->error('该会员编号已存在！');
            exit;
        }
        $kk = stripos($fwhere['user_id'],'-');
        if($kk){
            $this->error('会员编号中不能有扛(-)符号！');
            exit;
        }
        unset($fwhere,$frs);

        $errmsg="";
        if(empty($_POST['wenti_dan'])){
            $errmsg.="<li>密保答案不能为空！</li>";
        }
        $this->assign('wenti',$_POST['wenti']);
        $this->assign('wenti_dan',$_POST['wenti_dan']);
// 		if(empty($_POST['lang'])){
// 			$errmsg.="<li>语言不能为空！</li>";
// 		}
// 		$this->assign('lang',$_POST['lang']);

// 		if(empty($_POST['countrys'])){
// 			$errmsg.="<li>国家不能为空！</li>";
// 		}
        $this->assign('countrys',$_POST['countrys']);


        if(empty($_POST['BankCard'])){
            $errmsg.="<li>银行卡号不能为空！</li>";
        }
        $bankNums=$fck->where("bank_card='".$_POST['BankCard']."'")->count();
        if($bankNums>0){
//			$this->error("同一个银行卡号最多只能注册1个账号");
//			exit;
        }
        $this->assign('BankCard',$_POST['BankCard']);
        $huhu=trim($_POST['UserName']);
        if(empty($huhu)){
            $errmsg.="<li>请填写开户姓名！</li>";
        }
        $namecount=$fck->where("user_name='".$_POST['UserName']."'")->count();
        if($namecount>0){
//			$this->error("同一个开户姓名最多只能注册1个账号");
//			exit;
        }
        $this->assign('UserName',$_POST['UserName']);
        if(empty($_POST['UserCode'])){
            $errmsg.="<li>请填写身份证号码！</li>";
        }
        //$usercc=trim($_POST['UserCode']);
        if(!preg_match("/\d{17}[\d|X]|\d{15}/",$_POST['UserCode'])){
//			$errmsg.="<li>身份证号码格式不正确！</li>";
        }
        $count_pho=$fck->where("user_code='".$_POST['UserCode']."'")->count();
        if($count_pho>0){
//			$this->error("同一个身份证号码最多只能注册1个账号");
//			exit;
        }
        $this->assign('UserCode',$_POST['UserCode']);

        if(empty($_POST['UserTel'])){
            $errmsg.="<li>请填写电话号码！</li>";
        }
        if(!preg_match("/^1[34578]\d{9}$/",$_POST['UserTel'])){
//			$this->error('电话号码格式不正确！');
//			exit;
        }
        $countNums=$fck->where('user_tel='.$_POST['UserTel'])->count();
        if($countNums>0){
//			$this->error("同一个手机最多只能注册1个账号");
//			exit;
        }
        $this->assign('UserTel',$_POST['UserTel']);
        if(empty($_POST['UserWeiXin'])){
            $errmsg.="<li>请填写微信号！</li>";
        }
        $this->assign('UserWeiXin',$_POST['UserWeiXin']);
        //if(empty($_POST['qq'])){
//			$errmsg.="<li>请填写QQ号码！</li>";
//		}
//		$this->assign('qq',$_POST['qq']);
// 		if(empty($_POST['UserEmail'])){
// 			$errmsg.="<li>请填写您的邮箱地址，找回密码时需使用！</li>";
// 		}
        $this->assign('UserEmail',$_POST['UserEmail']);

        if(strlen($_POST['Password']) < 1 or strlen($_POST['Password']) > 16 or strlen($_POST['PassOpen']) < 1 or strlen($_POST['PassOpen']) > 16){
            $this->error('密码应该是1-16位！');
            exit;
        }
        if($_POST['Password'] != $_POST['rePassword']){  //一级密码
            $this->error('一级密码两次输入不一致！');
            exit;
        }
        if($_POST['PassOpen'] != $_POST['rePassOpen']){  //二级密码
            $this->error('二级密码两次输入不一致！');
            exit;
        }
        if($_POST['Password'] == $_POST['PassOpen']){  //二级密码
            $this->error('一级密码与二级密码不能相同！');
            exit;
        }
        $this->assign('Password',$_POST['Password']);
        $this->assign('PassOpen',$_POST['PassOpen']);

//		if($_POST['BankProvince'] == "请选择"){  //省份
//			$this->error('请选择省份！');
//			exit;
//		}
//		if($_POST['BankCity'] == "请选择"){  //城市
//			$this->error('请选择城市！');
//			exit;
//		}


        $us_name = $_POST['us_name'];
        $us_address = $_POST['us_address'];
        $us_tel = $_POST['us_tel'];
// 		if(empty($us_name)){
// 			$errmsg.="<li>请输入收货人姓名！</li>";
// 		}
// 		if(empty($us_address)){
// 			$errmsg.="<li>请输入收货地址！</li>";
// 		}
// 		if(empty($us_tel)){
// 			$errmsg.="<li>请输入收货人电话！</li>";
// 		}
        $this->assign('us_name',$_POST['us_name']);
        $this->assign('us_address',$_POST['us_address']);
        $this->assign('us_tel',$_POST['us_tel']);

        $s_err = "<ul>";
        $e_err = "</ul>";
        if(!empty($errmsg)){
            $out_err = $s_err.$errmsg.$e_err;
            $this->error($out_err);
            exit;
        }


        $uLevel = $_POST['u_level'];
        $this->assign('u_level',$uLevel);
        $fee  = M ('fee') -> find();
        $s    = $fee['s9'];
        $s10 = explode('|',$fee['s10']);
        $this->assign('uarray',$s10);
        $s9 = explode('|',$fee['s9']);

        $u_money = $s9[$uLevel];

        //如果是1单，就要限制，多单的不限制 start
        //选择第一个，也就是5000的
        if($uLevel==9999999999){
            $TelNums=$fck->where('user_tel='.$_POST['UserTel'])->count();
            if($TelNums>0){
                $this->error("同一个手机号最多只能注册1个账号");
                exit;
            }
            $bankNums=$fck->where("bank_card='".$_POST['BankCard']."'")->count();
            if($bankNums>0){
                $this->error("同一个银行卡号最多只能注册1个账号");
                exit;
            }
            $codeNums=$fck->where("user_code='".$_POST['UserCode']."'")->count();
            if($codeNums>0){
                $this->error("同一个身份证最多只能注册1个账号");
                exit;
            }
            $namecount=$fck->where("user_name='".$_POST['UserName']."'")->count();
            if($namecount>0){
                $this->error("同一个开户姓名最多只能注册1个账号");
                exit;
            }
        }
        //如果是1单，就要限制，多单的不限制 end


        //======产品========
// 		$product = M ('product');
// 		$ydate = time();
// 		$cpid = $_POST['uid'];//所以产品的ID
// 		if (empty($cpid)){
// 			$this->error('请选择产品！');
// 			exit;
// 		}
// 		$pro_where = array();
// 		$pro_where['id'] = array ('in',$cpid);
// 		$pro_rs = $product->where($pro_where)->field('id,money,a_money,name')->select();
// 		$cpmoney = 0;//产品总价
// 		$cparray = array();
// 		$txt = "";
// 		$cpi = 0;
// 		foreach ($pro_rs as $pvo){
// 			$aa = "shu".$pvo['id'];
// 			$cc = (int)$_POST[$aa];
// 			if ($cc >0) {
// 				$cpmoney = $cpmoney + $pvo['money'] * $cc;
// 				$txt .= $pvo['id'] .',';
// 				$cparray[$cpi]['id'] = $pvo['id'];
// 				$cparray[$cpi]['money'] = $pvo['money'];
// 				$cparray[$cpi]['name'] = $pvo['name'];
// 				$cparray[$cpi]['buynub'] = $cc;
// 				$cpi++;
// 			}
// 		}
// 		unset($product,$pro_rs);
// 		$this->assign('cparray',$cparray);//产品
// 		if($cpmoney!=$u_money){
// 			$this->error('产品金额和级别对不上，请重新选择！');
// 			exit;
// 		}
// 		$this->assign('cpmoney',$cpmoney);
        //======产品END=====


        $this->assign('BankName',$_POST['BankName']);
        $this->assign('BankProvince',$_POST['BankProvince']);
        $this->assign('BankCity',$_POST['BankCity']);
        $this->assign('BankAddress',$_POST['BankAddress']);

        $this->assign('UserAddress',$_POST['UserAddress']);
        $this->assign('qq',$_POST['qq']);

        $this->display();

    }
        //前台激活会员
        public function open_users(){
        $fck = M('fck');
        $fee=M('fee');
        $fee_rs=$fee->find();
        $s10=explode('|',$fee_rs['s10']);//会员等级
        $fck_rs = $fck->where('id='.$_SESSION[C('USER_AUTH_KEY')])->find();
        $this->assign('fck_rs',$fck_rs);
        $UserID = $_REQUEST['UserID'];

        $map = array();
        if (!empty($UserID)){
            $where['user_id'] = array('like',"%".$UserID."%");
            $where['_logic']    = 'or';
            $map['_complex']    = $where;
        }

        $map['is_pay'] = array('eq',0);

        import ( "@.ORG.ZQPage" );  //导入分页类
        $count = $fck->where($map)->count();//总页数
        $listrows = 10;//每页显示的记录数
        $Page = new ZQPage($count, $listrows, 1, 0, 3);
        //===============(总页数,每页显示记录数,css样式 0-9)
        $show = $Page->show();//分页变量
        $this->assign('page',$show);//分页变量输出到模板
        $list = $fck->where($map)->field('*')->order('rdt desc,id desc')->page($Page->getPage().','.$listrows)->select();
        $this->assign('list',$list);
        $this->assign('s10',$s10);
        $this->display();
    }

    //前台激活操作
    public function pay_reg(){
        $fck = D ('Fck');
        $id = $_GET['id'];
        $voo = $fck->where('id='.$id)->find();
        if ($voo['is_pay']>0){
            $this->error('该会员已激活，无需重复操作！');
            exit;
        }

        $fck_my = $fck->where('id='.$_SESSION[C('USER_AUTH_KEY')])->find();
        if ($fck_my['agent_use'] < $voo['cpzj']){
            $this->error('电子币余额不足！');
            exit;
        }
        // $fck->execute("update __TABLE__ set agent_use=agent_use-".$voo['cpzj']." where id=".$fck_my['id']);

        $shouru = M ('shouru');
        $fee = M('fee');
        $fee_rs = $fee->field('s2,s3,s9,s11')->find();
        $s2 = explode("|",$fee_rs['s2']);//级别单数
        $s3 = explode("|",$fee_rs['s3']);//配送比例
        $s9 = explode("|",$fee_rs['s9']);//投资金额

        $nowdate = strtotime(date('c'));
        $nowday = strtotime(date('Y-m-d'));


            //上级未开通不能开通下级员工
            $frs_where['is_pay'] = array('eq',0);
            $frs_where['id'] = $voo['father_id'];
            $frs = $fck -> where($frs_where) -> find();
            if($frs){
                $this->error('开通失败，上级未开通');
                exit;
            }

            //给推荐人添加推荐人数或单数
            $fck->query("update __TABLE__ set `re_nums`=re_nums+1,re_f4=re_f4+".$voo['f4']." where `id`=".$voo['re_id']);
            // $fck->query("update __TABLE__ set `agent_use`=agent_use+".$voo['cpzj']." where `id`=".$voo['id']);

            $nnrs = $fck->where('is_pay>0')->field('n_pai')->order('n_pai desc')->find();
            $mynpai = ((int)$nnrs['n_pai'])+1;

            //生成物流订单
            $gouwu=A('Gouwu');
            $gouwu2=M('gouwu');
            $gwd['order_id']=$gouwu->_getPPOrderID();
            $w_money = $voo['cpzj'];
            $gwd['did'] = 1;
            $gwd['lx'] = 1;
            $gwd['have_pay'] = 1;
            $gwd['pdt'] = time();
            $gwd['have_pay_time'] = time();
            $gwd['user_id'] = $voo['user_id'];
            $gwd['uid'] = $id;
            $gwd['money'] = $w_money;
            $gwd['shu'] = 1;
            $gwd['yifanli'] = $voo['cpzj'];
            $gwd['cprice'] = $w_money;
            $gwd['us_name'] = $voo['user_name'];
            $gwd['us_address'] = $voo['receive_address'];
            $gwd['us_tel'] = $voo['receive_tel'];
            $gouwu2->add($gwd);


            $in_gp = 0;//获得的股票金额

            //判断是否是第XX个会员，不是就不开通自身商店补贴（除去100000号自身）
            $num=$fck->where('is_pay=1 and id>1 and u_level='.$voo['u_level'])->count();
            $is_aa=0;
            if($num<=$fee_rs['s11']-1){
                $is_aa=1;
            }

            $data = array();
            $data['is_pay'] = 1;
            $data['is_aa'] = $is_aa;//是否发放自身商店补贴
            $data['pdt'] = $nowdate;
            $data['open'] = 1;
            $data['get_date'] = $nowday;
            $data['fanli_time'] = $nowday;//当天没有分红奖
            $data['agent_lock'] = $in_gp;//
//              $data['agent_gp'] = $in_gp;//
            $data['gp_num'] = $in_gp;//
            $data['n_pai'] = $mynpai;
            $data['is_zy']  = $voo['id'];
            $data['pay_time']  = $nowday;   //当天不给分红

            //开通会员
            $result = $fck->where('id='.$voo['id'])->save($data);
            unset($data,$varray);

            $data = array();
            $data['uid'] = $voo['id'];
            $data['user_id'] = $voo['user_id'];
            $data['in_money'] = $voo['cpzj'];
            $data['in_time'] = time();
            $data['in_bz'] = "新会员加入-前台开通";
            // $shouru->add($data);
            unset($data);


            //统计单数
            // $fck->xiangJiao($voo['id'],$voo['f4']);

            //算出奖金
            // $fck->getusjj($voo['id'],1);


            if ($result){
                //添加记录
                // $fck->addencAdd($fck_my['id'],$fck_my['user_id'], -$voo['cpzj'],19);
                A('Common')->zijin_log($fck_my['id'],$fck_my['user_id'],"pay_user","{$user_id}激活会员支出{$voo['cpzj']}",-$voo['cpzj'],"agent_use");
                $bUrl = __URL__.'/open_users';
                $this->_box(1,'激活成功！',$bUrl,1);
            }else{
                $this->error('激活失败！');
                exit();
            }


    }

    /**
     * 注册处理
     * **/
    public function usersAdd() {
        $this->_checkUser();
        $id = $_SESSION[C('USER_AUTH_KEY')];
        $fck    = M ('fck');  //注册表


        $rs = $fck -> field('is_pay,agent_cash') -> find($id);
        $m = $rs['agent_cash'];
        if($rs['is_pay'] == 0){
            $this->error('临时会员不能注册会员！');
            exit;
        }
        if (strlen($_POST['UserID'])<1){
            $this->error('会员编号不能少！');
            exit;
        }

        $data = array();  //创建数据对象
        //检测报单中心
        $shopid = trim($_POST['shopid']);  //所属报单中心帐号
        if (empty($shopid)){
            $this->error('请输入报单中心编号！');
            exit;
        }
        $smap = array();
        $smap['user_id'] = $shopid;
        $smap['is_agent'] = array('gt',1);
        $shop_rs = $fck->where($smap)->field('id,user_id')->find();
        if (!$shop_rs){
            $this->error('没有该报单中心！');
            exit;
        }else{
            $data['shop_id']   = $shop_rs['id'];      //隶属会员中心编号
            $data['shop_name'] = $shop_rs['user_id']; //隶属会员中心帐号
        }
        unset($smap,$shop_rs,$shopid);

        //检测推荐人
        $RID = trim($_POST['RID']);  //获取推荐会员帐号
        $mapp  = array();
        $mapp['user_id']	= $RID;
        $mapp['is_pay']	    = array('gt',0);
        $authInfoo = $fck->where($mapp)->field('id,user_id,re_level,re_path')->find();
        if ($authInfoo){
            $data['re_path'] = $authInfoo['re_path'].$authInfoo['id'].',';  //推荐路径
            $data['re_id'] = $authInfoo['id'];                              //推荐人ID
            $data['re_name'] = $authInfoo['user_id'];                       //推荐人帐号
            $data['re_level'] = $authInfoo['re_level'] + 1;                 //代数(绝对层数)
        }else{
            $this->error('推荐人不存在！');
            exit;
        }
        unset($authInfoo,$mapp);

        //检测上节点人
        //$FID = trim($_POST['FID']);  //上节点帐号
// 		$mappp  = array();
// 		$mappp['user_id'] = $FID;
//// 		$mappp['is_pay']  = array('gt',0);
// 		$authInfoo = $fck->where($mappp)->field('id,p_path,p_level,user_id,is_pay,tp_path')->find();
// 		if ($authInfoo){
// 			$fatherispay = $authInfoo['is_pay'];
// 			$data['p_path'] = $authInfoo['p_path'].$authInfoo['id'].',';  //绝对路径
// 			$data['father_id'] = $authInfoo['id'];                        //上节点ID
// 			$data['father_name'] = $authInfoo['user_id'];                 //上节点帐号
// 			$data['p_level'] = $authInfoo['p_level'] + 1;                 //上节点ID
// 			$tp_path = $authInfoo['tp_path'];
// 		}else{
// 			$this->error('上级会员不存在！');
// 			exit;
// 		}
// 		unset($authInfoo,$mappp);
//  		$TPL = (int)$_POST['TPL'];
// 		$where = array();
// 		$where['father_id'] = $data['father_id'];
// 		$where['treeplace'] = $TPL;
// 		$rs = $fck->where($where)->field('id')->find();
// 		if ($rs){
// 			$this->error('该位置已经注册！');
// 			exit;
// 		}else{
// 			$data['treeplace'] = $TPL;
// 			if(strlen($tp_path)==0){
// 				$data['tp_path'] = $TPL;
// 			}else{
// 				$data['tp_path'] = $tp_path.",".$TPL;
// 			}
// 		}
//
// 		if($fatherispay==0&&$TPL>0){
// 			$this->error('接点人开通后才能在此位置注册！');
// 			exit;
// 		}
//
//		$renn = $fck->where('re_id='.$data['re_id'].' and is_pay>0')->count();
//		if($renn<1){
//			$tjnn = $renn+1;
//			if($renn==0){
//				$oktp = 0;
//				$errtp = "左区";
//			}
//			$zz_id = $this->pd_left_us($data['re_id'],$oktp);
//			$zz_rs = $fck->where('id='.$zz_id)->field('id,user_id')->find();
//			if($zz_id!=$data['father_id']){
//				$this->error('推荐第'.$tjnn.'人必须放在'.$zz_rs['user_id'].'的'.$errtp.'！');
//				exit;
//			}
//			if($TPL!=$oktp){
//				$this->error('推荐第'.$tjnn.'人必须放在'.$zz_rs['user_id'].'的'.$errtp.'！');
//				exit;
//			}
//		}
//		unset($rs,$where,$TPL);

        $fwhere = array();//检测帐号是否存在
        $fwhere['user_id'] = trim($_POST['UserID']);
        $frs = $fck->where($fwhere)->field('id')->find();
        if ($frs){
            $this->error('该会员编号已存在！');
            exit;
        }
        $kk = stripos($fwhere['user_id'],'-');
        if($kk){
            $this->error('会员编号中不能有扛(-)符号！');
            exit;
        }
        unset($fwhere,$frs);

        $errmsg="";
        if(empty($_POST['wenti_dan'])){
            $errmsg.="<li>密保答案不能为空！</li>";
        }
        if(empty($_POST['BankCard'])){
            $errmsg.="<li>银行卡号不能为空！</li>";
        }
        $bankNums=$fck->where("bank_card='".$_POST['BankCard']."'")->count();
        if($bankNums>0){
//			$this->error("同一个银行卡号最多只能注册1个账号");
//			exit;
        }
        $huhu=trim($_POST['UserName']);
        if(empty($huhu)){
            $errmsg.="<li>请填写开户姓名！</li>";
        }
        $namecount=$fck->where("user_name='".$_POST['UserName']."'")->count();
        if($namecount>0){
//			$this->error("同一个开户姓名最多只能注册1个账号");
//			exit;
        }
        if(empty($_POST['UserCode'])){
            $errmsg.="<li>请填写身份证号码！</li>";
        }
        if(empty($_POST['UserTel'])){
            $errmsg.="<li>请填写电话号码！</li>";
        }
        if(!preg_match("/^1[34578]\d{9}$/",$_POST['UserTel'])){
//			$this->error('电话号码格式不正确！');
//			exit;
        }
        $countNums=$fck->where('user_tel='.$_POST['UserTel'])->count();
        if($countNums>0){
//			$this->error("同一个手机最多只能注册1个账号");
//			exit;
        }
        if(empty($_POST['UserWeiXin'])){
            $errmsg.="<li>请填写微信号！</li>";
        }
        //$this->assign('UserWeiXin',$_POST['UserWeiXin']);
        //if(empty($_POST['qq'])){
//			$errmsg.="<li>请填写QQ号码！</li>";
//		}
// 		if(empty($_POST['UserEmail'])){
// 			$errmsg.="<li>请填写您的邮箱地址，找回密码时需使用！</li>";
// 		}

        $usercc=trim($_POST['UserCode']);
        $count_pho=$fck->where("user_code='".$usercc."'")->count();
        if($count_pho>0){
//			$this->error("同一个身份证号码最多只能注册1个账号");
//			exit;
        }

        if(strlen($_POST['Password']) < 1 or strlen($_POST['Password']) > 16 or strlen($_POST['PassOpen']) < 1 or strlen($_POST['PassOpen']) > 16){
            $this->error('密码应该是1-16位！');
            exit;
        }
        if($_POST['Password'] == $_POST['PassOpen']){  //二级密码
            $this->error('一级密码与二级密码不能相同！');
            exit;
        }

        $us_name = $_POST['us_name'];
        $us_address = $_POST['us_address'];
        $us_tel = $_POST['us_tel'];
// 		if(empty($us_name)){
// 			$errmsg.="<li>请输入收货人姓名！</li>";
// 		}
// 		if(empty($us_address)){
// 			$errmsg.="<li>请输入收货地址！</li>";
// 		}
// 		if(empty($us_tel)){
// 			$errmsg.="<li>请输入收货人电话！</li>";
// 		}

        $this->assign('us_name',$_POST['us_name']);
        $this->assign('us_address',$_POST['us_address']);
        $this->assign('us_tel',$_POST['us_tel']);


        $s_err = "<ul>";
        $e_err = "</ul>";
        if(!empty($errmsg)){
            $out_err = $s_err.$errmsg.$e_err;
            $this->error($out_err);
            exit;
        }

        $uLevel = $_POST['u_level'];
        $fee  = M ('fee') -> find();
        $s    = $fee['s9'];
        $s2 = explode('|',$fee['s2']);
        $s9 = explode('|',$fee['s9']);

        $F4     = $s2[$uLevel];//认购单数
        $ul     = $s9[$uLevel];

        $Money = explode('|',C('Member_Money'));  //注册金额数组

        $new_userid = $_POST['UserID'];


//        $arry=$this->gongpaixtsmall($data['re_id']);
        $arry=$this->gongpaixtsmall(1);
        $data['father_id']             =$arry['father_id'];
        $data['father_name']             =$arry['father_name'];
        $data['treeplace']             =$arry['treeplace'];
        $data['p_level']             =$arry['p_level'];
        $data['p_path']             =$arry['p_path'];
        $data['u_pai']             =$arry['u_pai'];

        $data['user_id']             = $new_userid;
        $data['bind_account']        = '3333';
        $data['last_login_ip']       = '';                            //最后登录IP
        $data['verify']              = '0';
        $data['status']              = 1;                             //状态(?)
        $data['type_id']             = '0';
        $data['last_login_time']     = time();                        //最后登录时间
        $data['login_count']         = 0;                             //登录次数
        $data['info']                = '信息';
        $data['name']                = '名称';
        $data['password']            = md5(trim($_POST['Password']));  //一级密码加密
        $data['passopen']            = md5(trim($_POST['PassOpen']));  //二级密码加密
        $data['pwd1']                = trim($_POST['Password']);       //一级密码不加密
        $data['pwd2']                = trim($_POST['PassOpen']);       //二级密码不加密

        $data['wenti']				= trim($_POST['wenti']);  //密保问题
        $data['wenti_dan']			= trim($_POST['wenti_dan']);  //密保答案

        $data['lang']           = $_POST['lang'];             //语言
        $data['countrys']           = $_POST['countrys']; //国家

        $data['bank_name']           = $_POST['BankName'];             //银行名称
        $data['bank_card']           = $_POST['BankCard'];             //帐户卡号
        $data['user_name']           = $_POST['UserName'];             //姓名
        $data['nickname']			  = $_POST['UserID'];//$_POST['nickname'];  //昵称
        $data['bank_province']       = $_POST['BankProvince'];  //省份
        $data['bank_city']           = $_POST['BankCity'];      //城市
        $data['bank_address']        = $_POST['BankAddress'];          //开户地址
        //$data['user_post']           = $_POST['UserPost']; 		   //
        $data['user_code']           = $_POST['UserCode'];             //身份证号码
        $data['user_address']        = $_POST['UserAddress'];          //联系地址
        $data['email']               = $_POST['UserEmail'];            //电子邮箱
        //$data['qq']              	 = $_POST['qq'];            	   //qq
        $data['user_phone']               = $_POST['UserWeiXin'];            //微信号
        $data['user_tel']            = $_POST['UserTel'];              //联系电话
        $data['is_pay']              = 1;                              //是否开通
        $data['rdt']                 = time();                         //注册时间
        //$data['pdt']                 = time();
        $data['u_level']             = $uLevel+1;                      //注册等级
//        $data['cpzj']                = $ul;                          //注册金额
        $data['cpzj']                = 0;                          //注册金额
        $data['f4']                  = $F4;							//单量
        $data['wlf']                 = 0;                              //网络费

        $result = $fck->add($data);
        unset($data,$fck);
        if($result) {

            M('fee')->query("update __TABLE__ set us_num=us_num+1");

            $_SESSION['new_user_reg_id'] = $result;

            echo "<script>window.location='".__URL__."/users_ok/';</script>";
            exit;
        }else{
            $this->error('会员注册失败！');
            exit;
        }
    }

    /**
     * 注册完成
     * **/
    public function users_ok(){
        $this->_checkUser();
        $gourl = __APP__."/Reg/users/";
        if(!empty($_SESSION['new_user_reg_id'])){

            $fck = M('fck');
            $fee_rs = M ('fee') -> find();

            $this -> assign('s8',$fee_rs['s8']);
            $this -> assign('alert_msg',$fee_rs['str28']);
            $this -> assign('s17',$fee_rs['s17']);

            $myrs = $fck->where('id='.$_SESSION['new_user_reg_id'])->find();
            $this->assign('myrs',$myrs);

            $this->assign('gourl',$gourl);
            unset($fck,$fee_rs);
            $this->display();
        }else{
            echo "<script>window.location='".$gourl."';</script>";
            exit;
        }
    }
	//注册协议
	public function plan2(){
		$form = M ('form');
		$plan = M ('plan');
		$fck = M('fck');
		$id = $_SESSION[C('USER_AUTH_KEY')];  //登录AutoId
		//检测推荐人
		$where = array();
		$where['id'] = $id;
		$where['is_pay'] = array('gt',0);
		$field ='id,user_id,nickname,us_img,is_agent,shop_name';
		$fck_rs = $fck ->where($where)->field($field)->find();
		//print_r($fck_rs['id']);
		//exit;

		$fid = 1;
		$vo = $plan->find($fid);
		$vo['content'] = stripslashes($vo['content']);//过滤掉反斜杠
		$this->assign ( 'vo', $vo );
		$webtitle = $this->plan_class();
		$this->assign('title',$webtitle[$fid]);
		
		//查询注册协议的内容
		$from_rs = $form->where('type=3')->order('create_time desc')->limit(1)->find();
		
		$this->assign('from_rs',$from_rs);
		
		$see = $_SERVER['HTTP_HOST'].__APP__;
		$see = str_replace("//","/",$see);
        $this->assign ( 'server', $see );
		$this->assign ( 'fck_rs',$fck_rs);
		$this->display();
	}

	//前台注册
	public function us_reg(){
		$fee = M ('fee');
		$fck = M('fck');
		$reid = $_REQUEST['rid'];

		$fee_rs = $fee->field('*')->find();
		//会员级别
		$s1 = $fee_rs['s1'];
		$s1 = explode('|',$s1);
		$this->assign('s1',$s1);
		//选择开户银行
		$bank = explode('|',$fee_rs['str29']);
		$this->assign('bank',$bank);
		//检测推荐人
		$where = array();
		$where['user_id'] = $reid;
		$where['is_pay'] = array('gt',0);
		$field ='id,user_id,nickname,us_img,is_agent,shop_name,re_path';
		$rs = $fck ->where($where)->field($field)->find();
        if(!$rs){
            $this->error("暂未激活,不能推广");
        }
        //会员级别
        $HYJJ="";
        $this->_levelConfirm($HYJJ,1);
        $this->assign('HYJJ',$HYJJ);//会员级别
        $HYNM="";
        $this->_getLevelConfirm($HYNM,1);
        $this->assign('HYNM',$HYNM);//会员级别
		$this->assign('rs',$rs);
		$this->assign('reid',$reid);
		$this->display();
	}

    /**
     * 这部分为前台注册处理
     */
	public function us_regAC() {
		$fck          = M('fck');  //注册表
        $reid         = $this->_post('RID');
        $userid       = $this->_post('userid');
        $Password     = $this->_post('Password');
        $Passwordpid  = $this->_post('shorePasswordpid');
        $PassOpen     = $this->_post('PassOpen');
        $rePassOpen   = $this->_post('rePassOpen');
        $BankName     = $this->_post('BankName');
        $BankCard     = $this->_post('BankCard');
        $UserName     = $this->_post('UserName');
        $account      = $this->_post('account');
        $UserTel      = $this->_post('UserTel');
        $u_level      = $this->_post('u_level');
        $Name         = $this->_post('Name');
        // if(!preg_match("/^1[3456789]{1}\d{9}$/",$userid)){
        //     $this->error('请输入正确的手机号');
        //     exit;
        // }
        if(empty($userid)){
            $this->error('编号不能空');
            exit;
        }
        if(empty($Name)){
            $this->error('用户名称不能空');
            exit;
        }
        if(empty($Password)){
            $this->error('密码不能空');
            exit;
        }
        if($Password!=$Passwordpid){
            $this->error('两次一级密码不一致');
            exit;
        }
        if(empty($PassOpen)){
            $this->error('二级密码不能空');
            exit;
        }
        if($PassOpen!=$rePassOpen){
            $this->error('两次二级密码不一致');
            exit;
        }

        /*if(empty($u_level)){
            $this->error('请选择注册级别');
            exit;
        }
		if(empty($BankName)){
            $this->error('开户银行不能空');
            exit;
        }
        if(empty($BankCard)){
            $this->error('银行卡号不能空');
            exit;
        }
        if(empty($UserName)){
            $this->error('真是姓名不能空');
            exit;
        }*/
		$data = array();  //创建数据对象
		$mapp  = array();
		$mapp['user_id']	= $reid;
		//$mapp['is_pay']	= 1;
		$authInfoo = $fck->where($mapp)->field('id,user_id,re_level,re_path')->find();
		if ($authInfoo){
			$data['re_path'] = $authInfoo['re_path'].$authInfoo['id'].',';  //推荐路径
			$data['re_id']   = $authInfoo['id'];                              //推荐人ID
			$data['re_name'] = $authInfoo['user_id'];                      //推荐人帐号
			$data['re_level'] = $authInfoo['re_level'] + 1;                 //代数(绝对层数)
		}else{
			$this->error('推荐人不存在！');
			exit;
		}
		unset($authInfoo,$mapp);
        //检测帐号是否存在
		$checkUser = array();
        $checkUser['user_id'] = trim($userid);
		$isSet = $fck->where($checkUser)->field('id')->find();
		if ($isSet){
			$this->error('当前编号已被注册');
			exit;
		}
		unset($checkUser,$isSet);
        //用户填写的单数
        $uLevel = $u_level;
        $fee  = M ('fee') -> find();
        $s2 = explode('|',$fee['s2']);
        $F4     = $s2[$uLevel]/$s2[0];//认购单数
        $ul     = $s2[($uLevel-1)];
		$data['user_id']             = $userid;
		$data['verify']              = '0';
		$data['status']              = 1;                             //状态(?)
		$data['type_id']             = '0';
		$data['last_login_time']     = time();                        //最后登录时间
		$data['login_count']         = 0;                             //登录次数
		$data['info']                = '信息';
		$data['name']                = $Name;
		$data['password']            = md5(trim($Password));  //一级密码加密
		$data['passopen']            = md5(trim($PassOpen));  //二级密码加密
		$data['pwd1']                = trim($Password);       //一级密码不加密
		$data['pwd2']                = trim($PassOpen);       //二级密码不加密

        //$data['user_name']           = $UserName;             //用户名称
		/*$data['bank_name']           = $BankName;             //银行名称
		$data['bank_card']           = $BankCard;             //帐户卡号
        $data['Name']                = $Name;             //姓名

        $data['account']             = trim($account);              //支付宝
		$data['user_tel']            = trim($UserTel);              // 联系电话*/
		$data['is_pay']              = 0;                              //是否开通
		$data['rdt']                 = time();                         //注册时间
		$data['pdt']                 = 0;
		/*$data['u_level']             = $uLevel;                      //注册等级*/
		/*$data['cpzj']                = 0;                          //注册金额*/
        //$data['f4']                  = $F4;							//单量


        $money = $this->getFeeRule(['money_tobe_service'])['money_tobe_service']['0'];  //正式会员金额
        $data['cpzj']                = $money;                          //注册金额

        $result = $fck->add($data);
		if($result) {
            $bUrl = U('Public/login');
            $this->success('注册成功！',$bUrl);
		}else{
			$this->error('会员注册失败！');
			exit;
		}
		unset($data,$fck);
	}
    //选择品牌1
    public function brand_info(){
        $cptype = M ('cptype');
        $fck = M('fck');

        $result=explode('|',htmlspecialchars($_GET['result']));//推荐的人

        $id=(int)$result['0'];//推荐的产品
        $brandId=(int)$result['1'];//推荐的产品

        $user=$fck->where(array('id'=>$id))->find();
        $brand=$cptype->where(array('id'=>$brandId))->find();
        if($user&&$brand){
            $produce=M('product');
            $produceBrand=$produce->where(array('cptype'=>$brandId))->select();

            $this->assign('user',$user);
            $this->assign('produceBrand',$produceBrand);
            $this->display();
        }else{
            $this->redirect('show_all_brands',array('id'=>$id));
        }
    }
    //选择品牌2
    public function show_all_brands(){
        $cptype = M ('cptype');
        $fck = M('fck');
        $id=(int)htmlspecialchars($_GET['id']);//推荐人
        $user=$fck->where(array('id'=>$id))->find();

        if($user){
            //所有的产品=================================================
            $product = M('cptype');
            $map = array();
            $map['id'] = array('gt',0);
            $orderBy = 'id asc';
            $field  = '*';
            $brand = $product->where($map)->field($field)->order($orderBy)->select();

            $brandId_upper=array();//里面的键值是拼音首字母

            foreach ($brand as $key=>$item){
                $k=getfirstchar($item['tpname']);
                $brandId_upper[$k][]=$item;
            }
            //排序$brandId_upper的键值
            ksort($brandId_upper);
//            var_dump($brandId_upper);
//            die;
            $this->assign('brand',$brandId_upper);//数据输出到模板
            $this->assign('id',$id);//数据输出到模板
            //============================================================

            //热门推荐=================================================
            $map = array();
            $map['id'] = array('gt',0);
            $map['status'] = array('egt',1);//热门推荐
            $orderBy = 'id asc';
            $field  = '*';
            $brand = $product->where($map)->field($field)->order($orderBy)->select();
            $this->assign('brand_hot',$brand);//数据输出到模板
            //============================================================
            $this->display();
        }else{
            $this->error('错误代码001=='.$id);
        }
    }
    //保存订单
    public function order_save(){
        $pora = M('product');
        $fck = D('Fck');

        //选择的产品id
        $ring = (int)$_POST['ringId'];
        $pro = $pora->where(array('id'=>$ring)) ->find();
        if(!$pro){ $this->error('产品错误!');}
        //选择的价格
        $prices = (int)$_POST['amount'];
        if(!$prices){ $this->error('价格错误');}
        //推广的人
        $Id = (int)$_POST['userId'];
        $fck_rs = $fck->where(array('id'=>$Id)) ->find();
        if(!$fck_rs){ $this->error('推广人错误');}
        //手机号码
        $phone = $_POST['phone'];
        $rephone = $_POST['rephone'];
        if($phone!=$rephone){
            $this->error('两次输入的手机号不相同！');
            exit;
        }
        if(!preg_match("/^1[34578]\d{9}$/",$phone)){
			$this->error('电话号码格式不正确！');
			exit;
        }

        //订单号
        $dingdanhao=date('Ymd').rand(1000000000,9999999999).'A';

        $gouwu = M('gouwu');
        $gwd = array();
        $gwd['ccxhbz'] = $dingdanhao;
        $gwd['uid'] = $fck_rs['id'];
        $gwd['user_id'] = $fck_rs['user_id'];
        $gwd['lx'] = 0;
        $gwd['ispay'] = 0;
        $gwd['pdt'] = time();
        $gwd['us_name'] = $fck_rs['user_name'];
        $gwd['us_address'] = 0;
        $gwd['us_tel'] = $phone;

        $gwd['did'] = $pro['id'];
        $gwd['money'] = $prices;
        $gwd['shu'] = 0;
        $gwd['cprice'] = $prices;
        $gwd['countid'] = 0;
        $gouwu->add($gwd);

        // 前往支付
        $url=U('Weixinpay/pay',array('out_trade_no'=>$dingdanhao));
        redirect($url);
//        $this->success('提交成功！');
    }
	//生成会员编号
	private function _getUserID(){
		$fck = M('fck');
		
		$mynn = ''.rand(1000000,9999999);
		
		$fwhere['user_id'] = $mynn;
		$frss = $fck->where($fwhere)->field('id')->find();
		if ($frss){
			return $this->_getUserID();
		}else{
			unset($fck,$fee);
			return $mynn;
		}
	}
	
	//判断最左区
	public function pd_left_us($uid,&$tp){
		$fck = M('fck');
		$c_l = $fck->where('father_id='.$uid.' and treeplace='.$tp.'')->field('id')->find();
		if($c_l){
			$n_id = $c_l['id'];
			$tp = 0;
			$ren_id = $this->pd_left_us($n_id,$tp);
		}else{
			$ren_id = $uid;
		}
		unset($fck,$c_l);
		return $ren_id;
	}
	
	//
	public function find_agent(){
		$fck = M('fck');
		$where = "is_agent=2 and is_pay>0";
		$s_echo = '<table width="100%" border="0" cellpadding="0" cellspacing="0" class="tab1"><tr><td>';
		$e_echo = '</td></tr></table>';
		$m_echo = "";
		$c_l = $fck->where($where)->field('user_id,user_name,shop_a')->select();
		foreach($c_l as $ll){
			$m_echo .= "<li><b>".$ll['user_id']."</b>(".$ll['user_name'].")<br>".$ll['shop_a']."</li>";
		}
		unset($fck,$c_l);
		echo $s_echo.$m_echo.$e_echo;
	}
	
	
	
	// 找回密码1
	public function find_pw() {
		$_SESSION['us_openemail']="";
		
		$fee = M ('fee');
		$fee_s = $fee->field('str99')->find();
		$wentilist = explode('|',$fee_s['str99']);
		
		$this->assign('wentilist',$wentilist);
		
		$this->display('find_pw');
	}

	// 找回密码2
	public function find_pw_s() {
		if(empty($_SESSION['us_openemail'])){
			if(empty($_POST['us_name']) && empty($_POST['xg_wenti']) && empty($_POST['xg_wenti_dan'])) {
				$_SESSION = array();
				$this->display('../Public/LinkOut');
				return;
			}
			echo $ptname=$_POST['us_name'];
			echo $us_wenti=$_POST['xg_wenti'];
			echo $us_wentidan=$_POST['xg_wenti_dan'];
			$fck = M('fck');
			$rs=$fck->where("user_id='".$ptname."'")->field('id,email,user_id,user_name,wenti,wenti_dan,pwd1,pwd2')->find();
            $fee = M ('fee');
            $fee_s = $fee->field('str99')->find();
            $wentilist = explode('|',$fee_s['str99']);
            
            $this->assign('wentilist',$wentilist);
			if ($rs==false){
				$errarry['err']='<font color=red>注：找不到此会员编号！</font>';
				$this->assign('errarry',$errarry);
				$this->display('find_pw');
			}else{
				//echo $rs['wenti'];
				//echo $rs['wenti_dan'];
				if($us_wenti==$rs['wenti'] && $us_wentidan==$rs['wenti_dan']){
					$_SESSION['us_openemail']=$ptname;
					$this->find_pw_e($ptname);
				}else{
					$errarry['err']='<font color=red>注：密码验证失败！</font>';
					$this->assign('errarry',$errarry);
					$this->display('find_pw');
				}
			}
		}else{
			$us_email=$_SESSION['us_openemail'];
			$this->find_pw_e($us_email);
		}
	}

	// 找回密码3
	public function find_pw_e($us_email) {
		$fck = M('fck');
		$rs=$fck->where("user_id='".$us_email."'")->field('id,pwd1,pwd2')->find();
		$mypwd = $rs['pwd1'];
		$myerpwd = $rs['pwd2'];
		
		$this->assign('mypwd',$mypwd);
		$this->assign('myerpwd',$myerpwd);
		$this->display('find_pw_s');
	}
	//三轨小公排
	public function gongpaixtsmall($uid){
		$fck = M ('fck');
		$mouid=$uid;
        $field = 'id,user_id,p_level,p_path,u_pai';
        $where = 'is_pay>0 and (p_path like "%,'.$mouid.',%" or id='.$mouid.')';

		$re_rs = $fck ->where($where)->order('p_level asc,u_pai+0 asc')->field($field)->select();
		$fck_where = array();
		foreach($re_rs as $vo){
			$faid=$vo['id'];
			$fck_where['is_pay']   = array('egt',0);
			$fck_where['father_id']   = $faid;
			$count = $fck->where($fck_where)->count();
			$sumtp = $fck->where($fck_where)->sum('treeplace');
			//print_r($count);
			//print_r($sumtp);
			if($count==0 && $sumtp==0){
				$father_id=$vo['id'];
				$father_name=$vo['user_id'];
				$TreePlace=0;
				$p_level=$vo['p_level']+1;
				$p_path=$vo['p_path'].$vo['id'].',';
				$u_pai=$vo['u_pai']*2+$TreePlace;

				$arry=array();
				$arry['father_id']=$father_id;
				$arry['father_name']=$father_name;
				$arry['treeplace']=$TreePlace;
				$arry['p_level']=$p_level;
				$arry['p_path']=$p_path;
				$arry['u_pai']=$u_pai;
				return $arry;
				break;
			}elseif($count==1){
				if($sumtp==0){
					$father_id=$vo['id'];
					$father_name=$vo['user_id'];
					$TreePlace=1;
					$p_level=$vo['p_level']+1;
					$p_path=$vo['p_path'].$vo['id'].',';
					$u_pai=$vo['u_pai']*2+$TreePlace;
	
					$arry=array();
					$arry['father_id']=$father_id;
					$arry['father_name']=$father_name;
					$arry['treeplace']=$TreePlace;
					$arry['p_level']=$p_level;
					$arry['p_path']=$p_path;
					$arry['u_pai']=$u_pai;
					return $arry;
					break;
				}
				if($sumtp==1){
					$father_id=$vo['id'];
					$father_name=$vo['user_id'];
					$TreePlace=0;
					$p_level=$vo['p_level']+1;
					$p_path=$vo['p_path'].$vo['id'].',';
					$u_pai=$vo['u_pai']*2+$TreePlace;
	
					$arry=array();
					$arry['father_id']=$father_id;
					$arry['father_name']=$father_name;
					$arry['treeplace']=$TreePlace;
					$arry['p_level']=$p_level;
					$arry['p_path']=$p_path;
					$arry['u_pai']=$u_pai;
					return $arry;
					break;
				}
			}
		}
		unset($fck_where,$sumtp,$count);
	}
	public function send_email($useremail,$title='',$body='')
	{

		require_once "stemp/class.phpmailer.php";
		require_once "stemp/class.smtp.php";

		$arra=array();

		$mail = new PHPMailer();
		$mail->IsSMTP();                  // send via SMTP
		$mail->Host  = "smtp.163.com";   // SMTP servers
		$mail->SMTPAuth = true;           // turn on SMTP authentication
		$mail->Username = "yuyangtaoyecn";     // SMTP username     注意：普通邮件认证不需要加 @域名
		$mail->Password = "yuyangtaoyecn666";          // SMTP password
		$mail->From  = "yuyangtaoyecn@163.com";        // 发件人邮箱
		$mail->FromName =  "传奇梦";    // 发件人
		$mail->CharSet  = "utf-8";              // 这里指定字符集！
		$mail->Encoding = "base64";
		$mail->AddAddress("".$useremail."","".$useremail."");    // 收件人邮箱和姓名
		//$mail->AddAddress("119515301@qq.com","text");    // 收件人邮箱和姓名
		$mail->AddReplyTo("".$useremail."","163.com");
		$mail->IsHTML(true);    // send as HTML
		$mail->Subject  = $title; // 邮件主题
		$mail->Body = "".$body."";// 邮件内容
		$mail->AltBody ="text/html";
//		$mail->Send();

		if(!$mail->Send())
		{
		echo "Message could not be sent. <p>";
		echo "Mailer Error: " . $mail->ErrorInfo;
		exit;
		}
		//echo "Message has been sent";
	}


    public function createActiveCode()
    {
        $fee = M('fee');
        $feers = $fee->field('str12')->find();
        $str12 = explode('|',$feers['str12']);
        $this->assign('str12',$str12);

        $moblie = $_POST['moblie'];
        $Num=1;

        for ($i=0; $i < $Num; $i++) {

            $card_no = $this->buildActiveCode();
            $_SESSION['YAN_ZHENG_MA'] = $card_no;
// 			$checkphone=M('checkphone');
// 			$data = array();
// 			//$data['phone']=$tel;
// //			$data['check_no']=$randnum;
// //			$data['fstime']=mktime();
// //			$data['gqtime']=mktime()+5*60;
// 			$data['bid'] = 0;
// 			$data['buser_id'] = $moblie;
// 			$data['use_time'] = 0;
// 			$data['c_type'] = 0;
// 			$data['is_use'] = 0;
// 			$checkphone->execute('LOCK TABLE __TABLE__ WRITE');
// 			$data['card_no'] = $card_no;
// 			$data['c_time'] = time();
// 			$checkphone->add($data);
// 			$checkphone->execute('UNLOCK TABLES');

            //发送短信通知
            $hymobile=$moblie;
            //$hymsgcontent="欢迎您注册会员,随机验证码为:".$card_no.",请勿告诉任何人！【".$str12[2]."】";
            $hymsgcontent="【轻松认购】欢迎您注册会员，验证码为:".$card_no.",请勿转发!";
//            $this->SendMessage($hymobile,$hymsgcontent);
            $this->new_send_sms($hymobile,$hymsgcontent);
        }


        $this->success(' ');
    }

    protected function buildActiveCode(){


        $baseChar = '0123456789';
        $bit = 6;
        $tStr = '';
        do
        {
            $tStr = '';
            for($i=0;$i<$bit;$i++)
            {
                $rnd = rand(0,strlen($baseChar)-1);
                $tStr .= $baseChar[$rnd];
            }
        }while(M('checkphone')->field('id')->where(array('card_no'=>$tStr))->find());
        return $tStr;
    }

    //推广页面
    public function tuiguang(){
        $id=$_SESSION[C('USER_AUTH_KEY')];
        $fck=M('fck');
        $fck_rs=$fck->where('id='.$id)->field('user_id')->find();
        // print_r($_SERVER);die;
        $host=$_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'];
        $this->assign('user_id',$fck_rs['user_id']);
        $this->assign('host',$host);
        $this->display();
    }

    public function twodemcode(){
        vendor("phpqrcode.phpqrcode");
        $id = $_SESSION[C('USER_AUTH_KEY')];
        $fck_rs = M('fck') ->where(array('id'=>$id)) ->field('user_id') ->find();
        $see = $_SERVER['HTTP_HOST'].__APP__;
        $see = str_replace("//","/",$see);
        $data ="http://$see/Reg/us_reg/rid/{$fck_rs['user_id']}";
        // 纠错级别：L、M、Q、H
        $level = 'L';
        // 点的大小：1到10,用于手机端4就可以了
        $size = 4;
        // 下面注释了把二维码图片保存到本地的代码,如果要保存图片,用$fileName替换第二个参数false

        // 生成的文件名
        ob_end_clean();//清空缓冲区
        QRcode::png($data, $false, $level, $size);
    }

    //前台注册
    public function agent_reg(){
        $fee = M ('fee');
        $fck = M('fck');
        $reid = $_REQUEST['rid'];   //推荐用户的USER_ID

        $fee_rs = $fee->field('*')->find();
        //会员级别
        $s1 = $fee_rs['s1'];
        $s1 = explode('|',$s1);
        $this->assign('s1',$s1);
        //选择开户银行
        $bank = explode('|',$fee_rs['str29']);
        $this->assign('bank',$bank);
        //检测推荐人
        $where = array();
        $where['user_id'] = $reid;
        $where['is_pay'] = array('gt',0);
        $field ='id,user_id,nickname,us_img,is_agent,shop_name,re_path';
        $rs = $fck ->where($where)->field($field)->find();
        if(!$rs){
            $this->error("暂未激活,不能推广");
        }


        $fee_self = $this->getFeeRule(['money_tobe_service'])['money_tobe_service'];    //投资金额
        $Agent_Us_Name = C('Agent_Us_Name');
        $Aname = explode("|",$Agent_Us_Name);

        $this->assign ( 'Aname', $Aname);
        $this->assign ( 'money_to_service', $fee_self);


        //会员级别
        $HYJJ="";
        $this->_levelConfirm($HYJJ,1);
        $this->assign('HYJJ',$HYJJ);//会员级别
        $HYNM="";
        $this->_getLevelConfirm($HYNM,1);
        $this->assign('HYNM',$HYNM);//会员级别
        $this->assign('rs',$rs);
        $this->assign('reid',$reid);
        $this->display();
    }

    /**
     * 这部分为代理中心注册处理
     * 代理中心注册， 这部分bug可能比较多， 模板省市，区 选择用JS插入，不是从数据库中取出
     * shop_a为省，shop_b为市，shop_c为区.
     * 当shop_a为市时，shop_c不必填写; shop_b为市时, shop_c必须填写
     */
    public function agent_regAC() {
        $fck          = M('fck');  //注册表
        $reid         = $this->_post('RID');
        $userid       = $this->_post('userid');
        $Password     = $this->_post('Password');
        $Passwordpid  = $this->_post('shorePasswordpid');
        $PassOpen     = $this->_post('PassOpen');
        $rePassOpen   = $this->_post('rePassOpen');
        $BankName     = $this->_post('BankName');
        $BankCard     = $this->_post('BankCard');
        $UserName     = $this->_post('UserName');
        $account      = $this->_post('account');
        $UserTel      = $this->_post('UserTel');
        $u_level      = $this->_post('u_level');
        $Name         = $this->_post('Name');


        /*dump($_POST);
        exit;*/
        $shoplx  = (int)$_POST['shoplx'];
        $shop_a  = $_POST['shop_a'];
        $shop_b  = $_POST['shop_b'];

        $shop_c  = $_POST['shop_c'];
        $content  = $_POST['content'];


        if(empty($userid)){
            $this->error('编号不能空');
            exit;
        }
        if(empty($Name)){
            $this->error('用户名称不能空');
            exit;
        }
        if(empty($Password)){
            $this->error('密码不能空');
            exit;
        }
        if($Password!=$Passwordpid){
            $this->error('两次一级密码不一致');
            exit;
        }
        if(empty($PassOpen)){
            $this->error('二级密码不能空');
            exit;
        }
        if($PassOpen!=$rePassOpen){
            $this->error('两次二级密码不一致');
            exit;
        }

        if($shoplx == 1) {  //市运营中心
            $shop_c = '';
        }

        if($shoplx==1){
            if($shop_a == "请选择"){
                $this->error('请输入市级中心区域!');
            }
        }else{
            if($shop_a == "请选择" || $shop_b == "请选择"){
                $this->error('请输入市级或者县/区中心区域!');
            }
        }

        if($_POST['shop_a']=='省份'){
            $this->error('请选择你的省份!');
        }
        if($_POST['shop_b'] == '地级市'){
            $this->error('请选择你的地级市');
        }
        if($shoplx == 2  && ($_POST['shop_c'] == '市、县级市') ){
            $this->error('请选择你的市、县级市');
        }

        $f_agent_city = ''; //上级的市运营中心

        $bqycount=0;
        if($shoplx==1){
            $bqycount = $fck->where("(is_agent>0 and shoplx = 1 and shop_a= '$shop_a' and shop_b='$shop_b')")->count();
            $fa_rs = $fck->where(['user_id' => $reid, 'is_pay' => 1, 'is_agent' => 0])->field('id')->find();
            if($fa_rs || !empty($fa_rs)) {
                $this->error('分享人是会员不能申请中心市级运营中心!');
                exit;
            }
        }elseif($shoplx==2){
            $bqycount = $fck->where("is_agent>0 and shoplx = 2 and (shop_a= '$shop_a' and shop_b= '$shop_b') or (shop_a= '$shop_a' and shop_b= '$shop_b' and shop_c= '$shop_c')")->count();
            /*if(strpos($shop_b,"市") > 0 && $shop_c == ''){
                $this->error('体验中心级别，(县,区)选择的是市，必须手动填写 体验中心的县/区!');
                exit;
            }*/
            //这部分判断需要注意
            /*if(strpos($shop_a,"市") <= 0 && $shop_c == ''){
                $this->error('体验中心级别，必须手动填写 体验中心的县/区!');
                exit;
            }*/

            //保存上级区域的市
            /*if(strpos($shop_a,"市") > 0) {
                $f_agent_city = $shop_a;
            } else {
                $f_agent_city = $shop_b;
            }*/
        }

        if($bqycount>0){
            $this->error('本区域的报单中心已经存在!');
            exit;
        }


        $data = array();  //创建数据对象
        $mapp  = array();
        $mapp['user_id']	= $reid;
        //$mapp['is_pay']	= 1;
        $authInfoo = $fck->where($mapp)->field('id,user_id,re_level,re_path')->find();
        if ($authInfoo){
            $data['re_path'] = $authInfoo['re_path'].$authInfoo['id'].',';  //推荐路径
            $data['re_id']   = $authInfoo['id'];                              //推荐人ID
            $data['re_name'] = $authInfoo['user_id'];                      //推荐人帐号
            $data['re_level'] = $authInfoo['re_level'] + 1;                 //代数(绝对层数)
        }else{
            $this->error('推荐人不存在！');
            exit;
        }
        unset($authInfoo,$mapp);
        //检测帐号是否存在
        $checkUser = array();
        $checkUser['user_id'] = trim($userid);
        $isSet = $fck->where($checkUser)->field('id')->find();
        if ($isSet){
            $this->error('当前编号已被注册');
            exit;
        }
        unset($checkUser,$isSet);
        //用户填写的单数
        $uLevel = $u_level;
        $fee  = M ('fee') -> find();
        $s2 = explode('|',$fee['s2']);
        $F4     = $s2[$uLevel]/$s2[0];//认购单数
        $ul     = $s2[($uLevel-1)];
        $data['user_id']             = $userid;
        $data['verify']              = '0';
        $data['status']              = 1;                             //状态(?)
        $data['type_id']             = '0';
        $data['last_login_time']     = time();                        //最后登录时间
        $data['login_count']         = 0;                             //登录次数
        $data['info']                = '信息';
        $data['name']                = $Name;
        $data['password']            = md5(trim($Password));  //一级密码加密
        $data['passopen']            = md5(trim($PassOpen));  //二级密码加密
        $data['pwd1']                = trim($Password);       //一级密码不加密
        $data['pwd2']                = trim($PassOpen);       //二级密码不加密

        //$data['user_name']           = $UserName;             //用户名称
        /*$data['bank_name']           = $BankName;             //银行名称
        $data['bank_card']           = $BankCard;             //帐户卡号
        $data['Name']                = $Name;             //姓名

        $data['account']             = trim($account);              //支付宝
        $data['user_tel']            = trim($UserTel);              //联系电话*/
        $data['is_pay']              = 0;                              //是否开通
        $data['rdt']                 = time();                         //注册时间
        $data['pdt']                 = 0;
        /*$data['u_level']             = $uLevel;                      //注册等级*/
        /*$data['cpzj']                = 0;                          //注册金额*/
        //$data['f4']                  = $F4;							//单量

        $data['f_agent_city']                 = $f_agent_city;

        $fee_self = $this->getFeeRule(['money_tobe_service'])['money_tobe_service'];
        if($shoplx==1) {
            $money = $fee_self['2'];
        } elseif($shoplx == 2) {
            $money = $fee_self['1'];
        } else {
            $money = 0;
        }

        $data['cpzj']                = $money;                          //注册金额
        $data['is_agent']            = 1;                          //代理
        $data['shoplx']            = $shoplx;                          //代理级别
        $data['shop_a']            = $shop_a;                          //代理级别，省
        $data['shop_b']            = $shop_b;                          //代理级别，市/区、县
        $data['shop_c']            = $shop_c;                          //代理级别，区/县
        $data['content']            = $content;                          //注册金额
        $data['idt']            = time();                          //注册金额

        $result = $fck->add($data);

        /*dump(M()->getDbError());
        dump(M()->getLastSql());*/
        if($result) {
            $bUrl = U('Public/login');
            $this->success('注册成功！',$bUrl);
        }else{
            $this->error('会员注册失败！');
            exit;
        }
        unset($data,$fck);
    }

    public function pinfo() {
        phpinfo();
    }
}
?>